import Vue from 'vue'
import Router from 'vue-router'
import Home from 'views/Home.vue'
import store from '@/store'

Vue.use(Router)

let routes = [{
  path: '/',
  name: 'home',
  component: Home,
  meta: {
    title: '首页',
    keepAlive: true
  }
}, {
  path: '/project',
  name: 'project',
  component: () => import( /* webpackChunkName: "404" */ 'views/Project.vue'),
  meta: {
    title: '项目',
    keepAlive: true
  }
}, {
  path: '/news',
  name: 'news',
  component: () => import( /* webpackChunkName: "404" */ 'views/News.vue'),
  meta: {
    title: '资讯',
    keepAlive: true
  }
}, {
  path: '/mine',
  name: 'mine',
  component: () => import( /* webpackChunkName: "404" */ 'views/Mine.vue'),
  meta: {
    title: '我的',
    keepAlive: true
  }
}, {
  path: '/auth',
  name: 'auth',
  component: () => import( /* webpackChunkName: "404" */ 'views/Auth/Index.vue'),
  meta: {
    title: '认证',
    keepAlive: true
  }
}, {
  path: '/personalAuth',
  name: 'personalAuth',
  component: () => import( /* webpackChunkName: "404" */ 'views/Auth/PersonalAuth.vue'),
  meta: {
    title: '个人认证',
  }
}, {
  path: '/enterpriseAuth',
  name: 'enterpriseAuth',
  component: () => import( /* webpackChunkName: "404" */ 'views/Auth/EnterpriseAuth.vue'),
  meta: {
    title: '企业认证',
  }
}, {
  path: '/404',
  name: '404',
  component: () => import( /* webpackChunkName: "404" */ 'views/404.vue'),
  meta: {
    title: '404',
    keepAlive: true
  }
}]

const routerContext = require.context('./', true, /\.js$/)
routerContext.keys().forEach(route => {
  // 如果是根目录的 index.js 、不处理
  if (route.startsWith('./index')) {
    return
  }
  const routerModule = routerContext(route)
  /**
   * 兼容 import export 和 require module.export 两种规范
   */
  routes = routes.concat(routerModule.default || routerModule)
})

routes = routes.concat({
  path: '*',
  redirect: '/404'
})

const createRouter = () => new Router({
  mode: 'history', // require service support
  base: process.env.BASE_URL,
  scrollBehavior: () => ({
    y: 0
  }),
  routes
})

const myRouter = createRouter()

// const myRouter = new Router({
//   mode: 'history',
//   base: process.env.BASE_URL,
//   routes
// })

const history = window.sessionStorage
history.clear()
let historyCount = history.getItem('count') * 1 || 0
history.setItem('/', 0)

myRouter.beforeEach((to, from, next) => {
  if (to.params.direction) {
    store.commit('updateDirection', to.params.direction)
  } else {
    const toIndex = history.getItem(to.path)
    const fromIndex = history.getItem(from.path)
    // 判断并记录跳转页面是否访问过，以此判断跳转过渡方式
    if (toIndex) {
      if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
        store.commit('updateDirection', 'forward')
      } else {
        store.commit('updateDirection', 'back')
      }
    } else {
      ++historyCount
      history.setItem('count', historyCount)
      to.path !== '/' && history.setItem(to.path, historyCount)
      store.commit('updateDirection', 'forward')
    }
  }
  next()
})

export function resetRouter() {
  myRouter.replace('/login')
}

export default myRouter
